/**
 * Author: zhezhen.cheng
 * Creation Time: 2016/6/14.
 * Description:...
 */
define(['jquery','artTemplate','../m/wirelessM','../../../common/js/lib/text!../../../tpl/noInser.tpl','./getSsidList','../m/linknetPostM', '../../../common/js/lib/text!../../../tpl/footer.tpl'],
    function ($,artTemplate,wirelessM,noInser,getSsidList,linknetPostM,footerT) {
        function wirelessV(datas,arg1) {
            if(datas.data.wan_type==-1){
                <!-- 无网线-->
                $('.webApp-content').html(_.template(noInser)());
            }else{
                var wan_set_status = (datas.data.wan_set_status==0) ? '' : 'warning';
                var wan_set_status_text = (datas.data.wan_set_status==0) ? '已连接' : '未连接';
                var wifi_ssid = (datas.data.wifi_ssid != null) ? datas.data.wifi_ssid : '';
                var wifi_password = (datas.data.wifi_password != null) ? datas.data.wifi_password : '';
                var wan_ip = (datas.data.wan_ip != null) ? datas.data.wan_ip : '';
                var wan_mask = (datas.data.wan_mask != null) ? datas.data.wan_mask : '';
                var wan_dns = (datas.data.wan_dns != null) ? datas.data.wan_dns : '';
                var wan_pppoe_user = (datas.data.wan_pppoe_user != null) ? datas.data.wan_pppoe_user : '';
                var wan_pppoe_password = (datas.data.wan_pppoe_password != null) ? datas.data.wan_pppoe_password : '';
                //初始化选中的值
                var wan_types = '';
                if(datas.data.wan_type == 0){
                    wan_types= 'DHCP';
                }else if(datas.data.wan_type == 2){
                    wan_types= '无线中继';
                }else{
                    wan_types= 'PPPOE';
                }
                var html = '';
                html+='<div class="header">';
                html+='<a href="/h5-wizard/index.html#advanced-setting" class="back-a"></a><p>连接网络</p><a class="header-right">保存</a>';
                html+='</div>';
                html+='<div class="content p-7-rem">';
                html+='<div class="contect-state m-t-6"><i>联网状态</i><span class="'+wan_set_status+'">'+wan_set_status_text+'</span></div>';
                html+='<div class="ly-pppoe">';
                html+='<li><i>联网方式</i><div class="selects selecter"><input type="text" class="link-mode" readonly="readonly" placeholder="请选择联网方式" value="'+wan_types+'" vals="'+datas.data.wan_type+'"><span class=""></span>';
                html+='<ul class="mode-list" style="display: none;">';
                //判断是否是充电宝（当充电宝的时候只有中继，其它两种没办法用，就是不可选的，但是如果是路由的话， 就DHCP与PPPOE可以互相换，但是中继就不能用 了）   0路由 1充电宝
                if(arg1 == 0){
                    html+='<li value="2" class="e-bg">无线中继</li>';
                    html+='<li value="0">DHCP</li>';
                    html+='<li value="1">PPPOE</li>';
                }else{
                    html+='<li value="2">无线中继</li>';
                    html+='<li value="0" class="e-bg">DHCP</li>';
                    html+='<li value="1" class="e-bg">PPPOE</li>';
                }
                html+='</ul>';
                html+='</div>';
                html+='</li>';
                html+='</ul>';
                html+='</div>';
                html+='<div class="chang-div">';
                <!-- 无线中继-->
                var iszhong = (datas.data.wan_type==2) ? '' : 'display-none';
                html+='<div class="ly-pppoe m-t-6 '+iszhong+'">';
                html+='<ul>';
                html+='<li style="overflow: inherit;"><i>WiFi名称</i><div class="select selecter"><input class="wifi_name getSsid" type="text" value="'+wifi_ssid+'" readonly="readonly" placeholder="请选择WiFi名称" vals="'+wifi_ssid+'"><span  class="getSsid"></span><ul class="name-list" style="display: none;"></ul></div></li>';
                html+='<li><i>WiFi密码</i><span><input class="wifi_pass" type="password" value="'+wifi_password+'"></span></li>';
                html+='</ul>';
                html+='</div>';
                <!-- DHCP-->
                var isDhcp = (datas.data.wan_type==0) ? '' : 'display-none';
                html+='<ul class="device-txt m-t-6 '+isDhcp+'">';
                html+='<li class="ok-mes"><i>IP地址</i><span class="wan_ip">'+wan_ip+'</span></li>';
                html+='<li class="ok-mes"><i>子网掩码</i><span class="wan_mask">'+wan_mask+'</span></li>';
                html+='<li class="ok-mes"><i>DNS地址</i><span class="wan_dns">'+wan_dns+'</span></li>';
                html+='</ul>';
                <!-- PPPOE-->
                var isPppoe = (datas.data.wan_type==1) ? '' : 'display-none';
                html+='<div class="ly-pppoe m-t-6 '+isPppoe+'">';
                html+='<ul>';
                html+='<li><i>宽带账号</i><span><input class="kuan_name" type="text" value="'+wan_pppoe_user+'"></span></li>';
                html+='<li><i>宽带密码</i><span><input class="kuan_pass" type="password" value="'+wan_pppoe_password+'"></span></li>';
                html+='</ul>';
                html+='</div>';
                html+= '</div>';
                var footer = artTemplate.compile(footerT);
                html += footer({type:4});
                $('.webApp-content').html(html);
                //这个是下拉点击函数
                getSsidList();
            }

            $(".webApp-content").on('click','.selects',function(event){
                var select_obj = $(this).find('ul');
                $(this).find('ul').hide();
                if(select_obj.css('display') == 'none'){
                    select_obj.show();
                }
                event.stopPropagation();
            });
            $(".webApp-content").on('click','.mode-list li',function(event){
                var vals = $(this).attr('value');
                var text_val = $(this).text();
                if($(this).hasClass('e-bg')) return false;
                $(".chang-div>div,.chang-div>ul").addClass('display-none');
                if($(this).index() == 0){
                    $(".chang-div>div:first-child").removeClass('display-none');
                }else if($(this).index() == 2){
                    $(".chang-div>div:last-child").removeClass('display-none');
                }else{
                    $(".chang-div>ul").removeClass('display-none');
                }
                $(this).closest('.selects').find('input').val(text_val);
                $(this).closest('.selects').find('input').attr('vals',vals);
                $(this).parent().hide();
            });
            $(document).click(function(event){
                $(".selects,.select").find('ul').hide();
            });

            $(".header-right").on('click',function(){
                var link_mode = parseInt($(".link-mode").attr('vals'));
                var wifissids = $(".wifi_name").val();
                var wifi_pass = $(".wifi_pass").val();
                var wifi_mac =  $(".wifi_name").attr('mac');
                var data = '';
                if(link_mode == 2){
                    data = JSON.stringify({
                        "wanstatus": link_mode,
                        "wifissid": wifissids,
                        "wifipass": wifi_pass,
                        "wifimac":wifi_mac
                    })
                }else if(link_mode == 0){
                    data = JSON.stringify({
                        "wanstatus": link_mode
                    })
                }else{
                    var kuan_name = $(".kuan_name").val();
                    var kuan_pass = $(".kuan_pass").val();
                    data = JSON.stringify({
                        "wanstatus": link_mode,
                        "pppname": kuan_name,
                        "password":kuan_pass
                    })
                }
                linknetPostM(data);
            });

        }

        return wirelessV;
    });
